Add a --no-run option to `cargo test`
authorAlex Crichton <alex@alexcrichton.com>
Thu, 21 Aug 2014 21:09:27 +0000 (14:09 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 27 Aug 2014 00:45:49 +0000 (17:45 -0700)
This allows tests to be built, but not run.

Makefile.in
src/bin/cargo-bench.rs
src/bin/cargo-test.rs
src/cargo/ops/cargo_test.rs
src/cargo/ops/mod.rs
tests/test_cargo_test.rs

index c9de8ceab7d01f4b91f9938e6787531c429114ba..739ca90834b6b88f2fd449c4946d090db3232f62 100644 (file)
@@ -85,7 +85,7 @@ style:
        sh tests/check-style.sh
 
 no-exes:
-       find $$(git ls-files) -perm +111 -type f \
+       find $$(git ls-files | grep -v ttf) -perm +111 -type f \
                -not -name configure -not -name '*.sh' -not -name '*.rs' | \
                grep '.*' \
                && exit 1 || exit 0
index e700c13e802c5959d0f1781acc322001bef29087..54ca632279581285c4d5e630a1ab4213932d5451 100644 (file)
@@ -21,7 +21,9 @@ Usage:
 
 Options:
     -h, --help              Print this message
+    --no-run                Compile, but don't run benchmarks
     -j N, --jobs N          The number of jobs to run in parallel
+    --target TRIPLE         Build for the target triple
     --manifest-path PATH    Path to the manifest to build benchmarks for
     -v, --verbose           Use verbose output
 
@@ -39,16 +41,19 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
     let root = try!(find_root_manifest_for_cwd(options.flag_manifest_path));
     shell.set_verbose(options.flag_verbose);
 
-    let mut compile_opts = ops::CompileOptions {
-        update: false,
-        env: "bench",
-        shell: shell,
-        jobs: options.flag_jobs,
-        target: None,
-        dev_deps: true,
+    let mut ops = ops::TestOptions {
+        no_run: options.flag_no_run,
+        compile_opts: ops::CompileOptions {
+            update: false,
+            env: "bench",
+            shell: shell,
+            jobs: options.flag_jobs,
+            target: options.flag_target.as_ref().map(|s| s.as_slice()),
+            dev_deps: true,
+        },
     };
 
-    let err = try!(ops::run_benches(&root, &mut compile_opts,
+    let err = try!(ops::run_benches(&root, &mut ops,
                                     options.arg_args.as_slice()).map_err(|err| {
         CliError::from_boxed(err, 101)
     }));
index 873c20eb09845c38ea262a38101afc16b2f46c79..40c495246f2da129ad11abfd04fdffb3afbc5a81 100644 (file)
@@ -21,6 +21,7 @@ Usage:
 
 Options:
     -h, --help              Print this message
+    --no-run                Compile, but don't run tests
     -j N, --jobs N          The number of jobs to run in parallel
     --target TRIPLE         Build for the target triple
     -u, --update-remotes    Deprecated option, use `cargo update` instead
@@ -40,16 +41,19 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
     let root = try!(find_root_manifest_for_cwd(options.flag_manifest_path));
     shell.set_verbose(options.flag_verbose);
 
-    let mut compile_opts = ops::CompileOptions {
-        update: options.flag_update_remotes,
-        env: "test",
-        shell: shell,
-        jobs: options.flag_jobs,
-        target: options.flag_target.as_ref().map(|s| s.as_slice()),
-        dev_deps: true,
+    let mut ops = ops::TestOptions {
+        no_run: options.flag_no_run,
+        compile_opts: ops::CompileOptions {
+            update: options.flag_update_remotes,
+            env: "test",
+            shell: shell,
+            jobs: options.flag_jobs,
+            target: options.flag_target.as_ref().map(|s| s.as_slice()),
+            dev_deps: true,
+        },
     };
 
-    let err = try!(ops::run_tests(&root, &mut compile_opts,
+    let err = try!(ops::run_tests(&root, &mut ops,
                                   options.arg_args.as_slice()).map_err(|err| {
         CliError::from_boxed(err, 101)
     }));
index d1c0ad13e7e47599159361a2c2f11bf76a90cefe..7e8995769c7356ba72281057345ddca4979da2ab 100644 (file)
@@ -5,14 +5,20 @@ use sources::PathSource;
 use ops;
 use util::{CargoResult, ProcessError};
 
+pub struct TestOptions<'a> {
+    pub compile_opts: ops::CompileOptions<'a>,
+    pub no_run: bool,
+}
+
 pub fn run_tests(manifest_path: &Path,
-                 options: &mut ops::CompileOptions,
-                 args: &[String]) -> CargoResult<Option<ProcessError>> {
+                 options: &mut TestOptions,
+                 test_args: &[String]) -> CargoResult<Option<ProcessError>> {
     let mut source = try!(PathSource::for_path(&manifest_path.dir_path()));
     try!(source.update());
     let package = try!(source.get_root_package());
 
-    let mut compile = try!(ops::compile(manifest_path, options));
+    let mut compile = try!(ops::compile(manifest_path, &mut options.compile_opts));
+    if options.no_run { return Ok(None) }
     compile.tests.sort();
 
     let cwd = os::getcwd();
@@ -21,11 +27,11 @@ pub fn run_tests(manifest_path: &Path,
             Some(path) => path,
             None => exe.clone(),
         };
-        let cmd = compile.process(exe, &package).args(args);
-        try!(options.shell.concise(|shell| {
+        let cmd = compile.process(exe, &package).args(test_args);
+        try!(options.compile_opts.shell.concise(|shell| {
             shell.status("Running", to_display.display().to_string())
         }));
-        try!(options.shell.verbose(|shell| {
+        try!(options.compile_opts.shell.verbose(|shell| {
             shell.status("Running", cmd.to_string())
         }));
         match cmd.exec() {
@@ -42,7 +48,7 @@ pub fn run_tests(manifest_path: &Path,
     });
 
     for (lib, name) in libs {
-        try!(options.shell.status("Doc-tests", name));
+        try!(options.compile_opts.shell.status("Doc-tests", name));
         let mut p = compile.process("rustdoc", &package)
                            .arg("--test").arg(lib)
                            .arg("--crate-name").arg(name)
@@ -51,8 +57,8 @@ pub fn run_tests(manifest_path: &Path,
                            .cwd(package.get_root());
 
         // FIXME(rust-lang/rust#16272): this should just always be passed.
-        if args.len() > 0 {
-            p = p.arg("--test-args").arg(args.connect(" "));
+        if test_args.len() > 0 {
+            p = p.arg("--test-args").arg(test_args.connect(" "));
         }
 
         for (pkg, libs) in compile.libraries.iter() {
@@ -64,7 +70,7 @@ pub fn run_tests(manifest_path: &Path,
             }
         }
 
-        try!(options.shell.verbose(|shell| {
+        try!(options.compile_opts.shell.verbose(|shell| {
             shell.status("Running", p.to_string())
         }));
         match p.exec() {
@@ -77,7 +83,7 @@ pub fn run_tests(manifest_path: &Path,
 }
 
 pub fn run_benches(manifest_path: &Path,
-                   options: &mut ops::CompileOptions,
+                   options: &mut TestOptions,
                    args: &[String]) -> CargoResult<Option<ProcessError>> {
     let mut args = args.to_vec();
     args.push("--bench".to_string());
index 16eb105e93dd2088254b2fd2996c9381be8229d3..71846ecf6dcb2dcafb0d974c382c9c7441c021a7 100644 (file)
@@ -7,7 +7,7 @@ pub use self::cargo_new::{new, NewOptions};
 pub use self::cargo_doc::{doc, DocOptions};
 pub use self::cargo_generate_lockfile::{generate_lockfile, write_resolve};
 pub use self::cargo_generate_lockfile::{update_lockfile, load_lockfile};
-pub use self::cargo_test::{run_tests, run_benches};
+pub use self::cargo_test::{run_tests, run_benches, TestOptions};
 
 mod cargo_clean;
 mod cargo_compile;
index 38980e77fe39630d50728149599db78635593d9f..2b4ed8a479c30e0c825d29f9b3f05954efc0ce01 100644 (file)
@@ -852,3 +852,25 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
                        fresh = FRESH,
                        dir = p.url()).as_slice()));
 })
+
+test!(test_no_run {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("src/lib.rs", "
+            #[test]
+            fn foo() { fail!() }
+        ");
+
+    assert_that(p.cargo_process("cargo-test").arg("--no-run"),
+                execs().with_status(0)
+                       .with_stdout(format!("\
+{compiling} foo v0.0.1 ({dir})
+",
+                       compiling = COMPILING,
+                       dir = p.url()).as_slice()));
+})